package models.System.Systems;

import models.System.Headline.code;
import models.SystemModels.SystemUser;
import models.SystemModels.SystemUserDetails;
import models.SystemModels.folders;
import play.Play;

import java.io.File;
import java.util.List;


/**
 * Created by Administrator on 2018-06-29.
 */
public class folder {
    /**
     * @首次创建文件夹
     * @param用户名
     * @param文件名称
     * @return0,1
     */
    private static int i;

    public static int folderCreate(String name,String files){
        List<SystemUser> systemUsers = SystemUser.find("UserName",name).fetch();
        Long id= systemUsers.get(0).Id;

        String file = String.format("%s\\design\\public\\folder",Play.applicationPath.getParentFile().getAbsoluteFile().toString());
        String userFileName =String.format("%s\\%s\\%s",file,id.toString(), files);
        String path =files;
        String paths = String.format("\\public\\folder\\%s\\%s",id.toString(),files);

        if ( userFolder(userFileName,id,path,paths)){
            return 1;
        }else{
            return 0;
        }
    }

    private static boolean userFolder(String path,Long id, String pathName,String paths){
        try {
            File files = new File(path);
            files.mkdirs();
            folders folders = new folders();
            folders.userId = id.toString();
            folders.folderName =pathName;
            folders.folderUrl = paths;
            folders.Images= null;
            folders.save();
            return false;
        }catch (Exception ex){
            ex.printStackTrace();
            return true;
        }
    }

    /**
     * @返回编号
     * @param name
     * @return
     */
    public static String userId(String name){
        List<SystemUser> systemUserList =  SystemUser.find("UserName",name).fetch();
        return systemUserList.get(0).Id.toString();
    }

    /**
     * @return path url
     * @param name
     * @return
     */
    public static String MyDocuments(String name){
        String userIds =userId(name);
        if (userIds== null){
            return  null;
        }else{
            List<SystemUserDetails> systemUserDetails = SystemUserDetails.find("UserId",Integer.parseInt(userIds)).fetch();
            if(systemUserDetails.isEmpty()){
                return  null;
            }else {
                String files = String.format("%s\\design\\public\\folder",Play.applicationPath.getParentFile().getAbsoluteFile().toString());
                String userFileName =String.format("%s\\%s\\%s",files,userId(name).toString(), code.SystemUserFile);
                File file = new File(userFileName);
                file.mkdirs();
                String path =files;
                String paths = String.format("\\public\\folder\\%s\\%s",files,userId(name),files);
                folders folders = new folders();
                folders.userId = userIds;
                folders.folderName =code.SystemUserFile;
                folders.folderUrl = paths;
                folders.Images= null;
                folders.save();
                return code.SystemUserFile;
            }

        }

    }

}
